/**
 * 小程序环境下获取状态栏+胶囊的高度
 * @returns {number}
 */
export const wxGetTopHeight = () => {
    const systemInfo = uni.getSystemInfoSync()
    const { statusBarHeight } = systemInfo || {}
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
    const { height: menuHeight } = menuButtonInfo || {}
    return px2Rpx(statusBarHeight) + px2Rpx(menuHeight)
}

/**
 * 获取更加安全距离的预留高度
 * @returns {*}
 */
export const wxGetSafeTopHeight = () => {
    /* #ifdef MP-WEIXIN */
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
    const {height, top} = menuButtonInfo || {}
    return px2Rpx(height) + px2Rpx(top)
    /* #endif */
    /* #ifndef MP-WEIXIN */
    return 0
    /* #endif */
}

export const wxGetCapsuleSize = () => {
    return uni.getMenuButtonBoundingClientRect()
}

/**
 * 小程序环境下获取胶囊按钮的坐标点
 * @returns {{top: number, left: number, bottom: number, right: number}}
 */
export const wxGetCapsulePosition = () => {
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
    const { top, right, bottom, left } = menuButtonInfo || {}
    return { top, right, bottom, left }
}

/**
 * px转rpx
 * @param px
 * @returns {number}
 */
export const px2Rpx = px => {
    const systemInfo = uni.getSystemInfoSync()
    const { windowWidth: screenWidth } = systemInfo || {}
    return px * 750 / screenWidth
}

export const rpx2px = rpx => uni.upx2px(rpx)

/**
 * 获取标题高度
 * @returns {number}
 */
export const getTitleViewHeight = () => {
    const statusHeight = uni.getSystemInfoSync().statusBarHeight
    return statusHeight + 130
}

/**
 * 小程序环境获取地址栏参数
 * @returns {{}}
 */
export const getUrlParamsInWeixin = () => {
    let curParam = getCurrentPages()[getCurrentPages().length - 1].options;
    let obj = {};
    for (let key in curParam) {
        obj[key] = curParam[key];
    }
    return obj;
}

/**
 * h5环境获取地址栏参数
 * @returns {object}
 */
export const getUrlParamsInH5 = () => {
    let url = location.href
    let reqParams = {}
    if (url.indexOf('?') !== -1) {
        let str = url.substring(url.indexOf('?') + 1)
        let strs = str.split('&')
        for (let i = 0; i< strs.length; i++) {
            reqParams[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
        }
    }
    return reqParams
}

/**
 * 获取固定长度的随机数字ID
 * @param idLength 需要生成的id长度
 * @returns {string}
 */
export const generateRandomNumId = idLength => {
    let randomId = ''
    let characters = '01234567889'

    for (let i =0; i< idLength; i++) {
        randomId += characters.charAt(Math.random() * characters.length)
    }
    return randomId
}
export const getCurrentPagesTotal = () => {
    return getCurrentPages().length
}

/**
 * 获取当前时间n天后的时间
 * @param afterDays
 * @param mode
 * @returns {`${number}-${string}-${string} ${string}:${string}:${string}`}
 */
export const getFormattedDateAfterDays = (afterDays = 0, mode = 'ymd') => {
    const curDate = new Date()
    curDate.setDate(curDate.getDate() + afterDays)

    const year = curDate.getFullYear()
    const month = curDate.getMonth() + 1
    const day = curDate.getDate()
    const hours = curDate.getHours()
    const min = curDate.getMinutes()
    const sec = curDate.getSeconds()

    let res
    if (mode === 'ymd') {
        res = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
    } else if (mode === 'ymdhms') {
        res = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${min.toString().padStart(2, '0')}:${sec.toString().padStart(2, '0')}`
    }
    return res
}

/**
 * 配置小程序打开分享功能
 */
export const wxSetUpShareFunction = (path) => {
    // alert(path);
    uni.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage','shareTimeline'],
        path : path,
    }).then(() => {
        console.log('设置分享成功')
    })
}
